package org.yxt.springcloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.yxt.springcloud.entities.CommonResult;
import org.yxt.springcloud.service.PaymentOpenFeignService;

/**
 * 使用openFeign执行RPC远程微服务调用
 *
 * @author Yang douya
 * @date 2022-01-02
 */
@Slf4j
@RestController
@RequestMapping(value = "openFeign/consumer/")
public class ConsumerOpenFeignController {

    @Autowired
    private PaymentOpenFeignService paymentOpenFeignService;

    /**
     * 使用OpenFeign调用远程数据
     *
     * @param id
     * @return
     */
    @GetMapping(value = "get/{id}")
    public CommonResult openFeign2Payment(@PathVariable long id) {
        return paymentOpenFeignService.getDateById(id);
    }

    /**
     * openFeign默认是1秒中，服务提供者必须响应数据，否则报错 Read timed out
     * 不是所有业务都能 在1秒内完成所有业务 所以需要自定义请求超时时间
     * openFeign默认使用Ribbon，所以设置超时实际就是修改Ribbon配置
     *
     * @return
     */
    @GetMapping(value = "timeout")
    public CommonResult timeout() {
        return paymentOpenFeignService.feignTimeout();
    }
}
